<?php

require_once( '../../libs/smarty.inc' );

require_once( '../../libs/smt_variables.php' );

require_once( '../../libs/Class_DB.php' );

require_once( '../../libs/Class_ERROR.php' );

require_once( '../../libs/login.inc.php' );

require_once( '../../libs/const.php' );



define('PAGE_MAX', 30 );



if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



$forms = $_GET;



if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){

	$page['current'] = $_GET['page'];

	$st = ($page['current'] -1) * PAGE_MAX;

}else{

	$page['current'] = 1;

	$st = 0;

}





$db = new Class_DB;





$sql = "SELECT * FROM D_SHIPMENT";

$cnt_sql	= "SELECT COUNT(*) AS cnt FROM D_SHIPMENT";

$query		= "";





/****  キーワード（製品名・納品先・依頼者名 ****/

$where1 = "";

if( isset($forms['keyword']) && $forms['keyword'] != '' ){

	$where1 = "(company LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR request_name LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR search_text LIKE '%" . $db->quote( $forms['keyword'] ) . "%') AND ";

	$query .= "&keyword=" . $forms['keyword'];

}



/****  伝票区分 ****/

$where2 = "";

if( isset($forms['denpyo']) && $forms['denpyo'] ){

	$where2 = sprintf("id IN (SELECT id FROM D_SHIPMENT_DETAIL WHERE denpyo = %u) AND ", $forms['denpyo'] );

	$query .= "&denpyo=" . $forms['denpyo'];

}



/****  フォロー記入 ****/

$where3 = ""; $where4 = "";

if( isset( $forms['follow'] ) ){

	if( in_array( 1, $forms['follow'] ) || in_array( 2, $forms['follow'] ) ){

		if( in_array( 1, $forms['follow'] ) ){

			$where3 = "(follow_text1 <> '' OR follow_text2 <> '') AND ";

			$query .= "&follow[]=1";

		}

		if( in_array( 2, $forms['follow'] ) ){

			$where4 = "(follow_text1 = '' AND follow_text2 = '') AND ";

			$query .= "&follow[]=2";

		}

	}

}



/**** 登録日 ****/

$where5 = ""; $where6 = "";

if( isset($forms['date1']) && $forms['date1'] != '' ){

	$where5 = sprintf("r_date >= '%s 00:00:00' AND ", $forms['date1'] );

	$query .= "&date1=" . $forms['date1'];

}

if( isset($forms['date2']) && $forms['date2'] != '' ){

	$where6 = sprintf("r_date <= '%s 23:59:59' AND ", $forms['date2'] );

	$query .= "&date2=" . $forms['date2'];

}



if( $where1 . $where2 . $where3 . $where4 . $where5 . $where6 != "" ){

	$sql .= " WHERE ";

	$sql .= $where1 . $where2 . $where3 . $where4 . $where5 . $where6;

	$sql = substr( $sql, 0, strlen($sql) -5 );



	$cnt_sql .= " WHERE ";

	$cnt_sql .= $where1 . $where2 . $where3 . $where4 . $where5 . $where6;

	$cnt_sql = substr( $cnt_sql, 0, strlen($cnt_sql) -5 );

}



$sql .=  sprintf(" ORDER BY r_date DESC LIMIT %u, %u", $st, PAGE_MAX );







// 件数取得・ページング

$db->query( $cnt_sql );

$row = $db->fetch();

$page['cnt'] = $row['cnt'];

$page['end'] = floor( $page['cnt'] / PAGE_MAX );

if( $page['cnt'] % PAGE_MAX ) $page['end']++;

$paging = array();

if( $page['end'] >= 15 ){

	$pagecnt = 0;

	for( $i = $page['current'] -7; $i <= $page['end']; $i++ ){

		if( $i > 0 ){

			$paging[] = $i;

			$pagecnt++;

		}

		if( $pagecnt >= 15 ) break;

	}



}else{

	// 全15件以下の場合は全件表示

	for( $i = 1; $i <= $page['end']; $i++ ){

		$paging[] = $i;

	}

}



$page['paging'] = $paging;

$page['item'] = PAGE_MAX;

$page['query'] = $query;





$db->query( $sql );

$data = array();

while( $row = $db->fetch() ){

	$data[] = $row;

}



foreach( $data as $k => $v ){

	$data[$k]['disp_id'] = 'B' . sprintf('%05d', $v['id']);

}



$smarty->assign('arr_follow', array( 1 => 'あり', 2 => 'なし' ) );

$smarty->assign('arr_denpyo', $arr_denpyo );

$smarty->assign('category', $category);

$smarty->assign('data', $data);

$smarty->assign('page', $page);

if( isset( $forms ) ) $smarty->assign('forms', $forms);

$smarty->display('web-admin/ship_index.html');



?>